/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.pbp.controller;

import ac.pbp.core.util.ViewHelper;
import ac.pbp.model.Buku;
import ac.pbp.model.User;
import ac.pbp.service.BukuService;
import ac.pbp.service.UserService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Kim
 */
@Controller
public class IndexController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private BukuService bukuService;
    
    @RequestMapping("/login.html")
    public ModelAndView login(HttpServletRequest request) {
        
        ModelAndView model = new ModelAndView("login");
        
        return model;
    }
    
    @RequestMapping("/dashboard.html")
    public ModelAndView newAdmin(HttpServletRequest request) {
        
        ModelAndView model = new ModelAndView("dashboard");
        
        
        return model;
    }
    
    @RequestMapping("/signin.html")
    public ModelAndView signin(HttpServletRequest request) {
        
        ModelAndView model = new ModelAndView("signin");
        model.addObject("error", request.getParameter("err"));
        
        return model;
    }
    
    @RequestMapping("/home.html")
    public ModelAndView home(HttpServletRequest request) {
        
        ModelAndView model = new ModelAndView("home");
        
        List<Buku> bukuList = bukuService.loadNewestBooks();

        model.addObject("first_name", request.getSession(false).getAttribute("first_name"));
        model.addObject("bukuList", bukuList);

        return model;
    }
    
    @RequestMapping("/checkUser.html")
    public ModelAndView checkUser(HttpServletRequest request) {
        
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        
        
        User user = userService.checkUser(email, password);
        
        if (user instanceof User) {
            request.getSession(false).setAttribute("name", user.getDisplayName());
            request.getSession(false).setAttribute("kodeUser", user.getKodeUser());
            request.getSession(false).setMaxInactiveInterval(1800);
            
            return ViewHelper.redirectTo("home.html");
        } else {
           return ViewHelper.redirectTo("signin.html");
        }
    }
    
    @RequestMapping("/signout.html")
    public ModelAndView signout(HttpServletRequest request) {
        
        request.getSession(false).removeAttribute("name");
        request.getSession(false).removeAttribute("kodeUser");
        
        return ViewHelper.redirectTo("login.html");
    }
    
    @RequestMapping("/signoutAdmin.html")
    public ModelAndView signoutAdmin(HttpServletRequest request) {
        
        request.getSession(false).removeAttribute("name");
        request.getSession(false).removeAttribute("kodeUser");
        
        return ViewHelper.redirectTo("loginAdmin.html");
    }
    
    @RequestMapping("/loginAdmin.html")
    public ModelAndView adminLogin(HttpServletRequest request) {
        
        ModelAndView model = new ModelAndView("loginAdmin");
         model.addObject("error", request.getParameter("err"));
        
        return model;
    }
    
      @RequestMapping("/checkAdmin.html")
    public ModelAndView checkAdmin(HttpServletRequest request) {
        
        String email = request.getParameter("email");
        String password = request.getParameter("password");

        
        User user = userService.checkAdmin(email, password);
          
        if (user instanceof User) {
            request.getSession(false).setAttribute("name", user.getDisplayName());
            request.getSession(false).setAttribute("kodeUser", user.getKodeUser());
            request.getSession(false).setMaxInactiveInterval(1800);

            return ViewHelper.redirectTo("dashboard.html");
        } else {
           return ViewHelper.redirectTo("loginAdmin.html?err=error");
        }
    }
}
